{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# GPyOpt: dealing with cost fuctions\n",
    "\n",
    "### Written by Javier Gonzalez, University of Sheffield.\n",
    "\n",
    "## Reference Manual index\n",
    "\n",
    "*Last updated Friday, 11 March 2016.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "GPyOpt allows to consider function evaluation costs in the optimization."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%pylab inline  \n",
    "import GPyOpt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# --- Objective function\n",
    "objective_true  = GPyOpt.objective_examples.experiments2d.branin()                 # true function\n",
    "objective_noisy = GPyOpt.objective_examples.experiments2d.branin(sd = 0.1)         # noisy version\n",
    "bounds = objective_noisy.bounds     \n",
    "objective_true.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "domain = [{'name': 'var_1', 'type': 'continuous', 'domain': bounds[0]}, ## use default bounds\n",
    "          {'name': 'var_2', 'type': 'continuous', 'domain': bounds[1]}]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def mycost(x):\n",
    "    cost_f  = np.atleast_2d(.1*x[:,0]**2 +.1*x[:,1]**2).T\n",
    "    cost_df = np.array([0.2*x[:,0],0.2*x[:,1]]).T\n",
    "    return cost_f, cost_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# plot the cost fucntion\n",
    "grid = 400\n",
    "bounds = objective_true.bounds\n",
    "X1 = np.linspace(bounds[0][0], bounds[0][1], grid)\n",
    "X2 = np.linspace(bounds[1][0], bounds[1][1], grid)\n",
    "x1, x2 = np.meshgrid(X1, X2)\n",
    "X = np.hstack((x1.reshape(grid*grid,1),x2.reshape(grid*grid,1)))\n",
    "\n",
    "cost_X, _ = mycost(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Feasible region\n",
    "plt.contourf(X1, X2, cost_X.reshape(grid,grid),100, alpha=1,origin ='lower')\n",
    "plt.title('Cost function')\n",
    "plt.colorbar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "GPyOpt.methods.BayesianOptimization?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from numpy.random import seed\n",
    "seed(123)\n",
    "BO = GPyOpt.methods.BayesianOptimization(f=objective_noisy.f,  \n",
    "                                            domain = domain, \n",
    "                                            initial_design_numdata = 5,\n",
    "                                            acquisition_type = 'EI',              \n",
    "                                            normalize_Y = True,\n",
    "                                            exact_feval = False,\n",
    "                                            acquisition_jitter = 0.05)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "seed(123)\n",
    "BO_cost = GPyOpt.methods.BayesianOptimization(f=objective_noisy.f,  \n",
    "                                            cost_withGradients = mycost,\n",
    "                                            initial_design_numdata =5,\n",
    "                                            domain = domain,                  \n",
    "                                            acquisition_type = 'EI',              \n",
    "                                            normalize_Y = True,\n",
    "                                            exact_feval = False,\n",
    "                                            acquisition_jitter = 0.05)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "BO.plot_acquisition()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "BO_cost.run_optimization(15)\n",
    "BO_cost.plot_acquisition()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "BO.run_optimization(15)\n",
    "BO.plot_acquisition()"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
